package com.ttbj.payment.original.wxpay.utils;
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.NodeList;

import com.ttbj.payment.original.wxpay.config.WxPayConfig;
import com.ttbj.util.Assert;

public class WXPayUtil {
    //生成随机字符串
    public static String getNonce_str() {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 15; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    //map转xml 加上签名信息
    public static String map2Xml(Map<String, Object> map) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer();
        StringBuilder sb2 = new StringBuilder();
        sb2.append("<xml>");
        for (String key : map.keySet()) {
            sb.append(key);
            sb.append('=');
            sb.append(map.get(key));
            sb.append('&');
            // sb2是用来做请求的xml参数
            sb2.append("<" + key + ">");
//            sb2.append("<![CDATA[" + map.get(key) + "]]>");
            sb2.append(map.get(key));
            sb2.append("</" + key + ">");
        }
        System.out.println("System.getenv(signKey)"+System.getenv("signKey"));
        sb.append(WxPayConfig.MCH_KEY);
        
        String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8");
        sb2.append("<sign>");
        sb2.append(sign);
        sb2.append("</sign>");
        sb2.append("</xml>");
        
        return sb2.toString();
    }
    //解析微信返回return_code SUCCESS或FILE
    //根据微信返回resultXml再次生成签名
    public static String getSign(Map<String, String> map) {
        StringBuffer sb = new StringBuffer();
        for (String key : map.keySet()) {
            sb.append(key);
            sb.append('=');
            sb.append(map.get(key));
            sb.append('&');
            
        }
         
        sb.append("key="+WxPayConfig.MCH_KEY);
        System.out.println("第二次签名内容:" + sb);
        System.out.println("第二次签名SING:" +MD5Util.MD5Encode(sb.toString(), "UTF-8"));
        return MD5Util.MD5Encode(sb.toString(), "UTF-8");
       
    }
    //解析微信返回return_code SUCCESS或FILE
    public static String getReturnCode(String resultXml) {
        String nonceStr;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = dbf.newDocumentBuilder();
            InputStream inputStream = new ByteArrayInputStream(resultXml.getBytes());
            org.w3c.dom.Document doc = builder.parse(inputStream); //
            // 下面开始读取
            org.w3c.dom.Element root = doc.getDocumentElement(); // 获取根元素
            NodeList nl = root.getElementsByTagName("return_code");
            org.w3c.dom.Element el = (org.w3c.dom.Element) nl.item(0);
            org.w3c.dom.Node nd = el.getFirstChild();
            nonceStr = nd.getNodeValue();
            return nonceStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //解析微信返回return_msg
    public static String getReturn_msg(String resultXml) {
        String nonceStr;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = dbf.newDocumentBuilder();
            InputStream inputStream = new ByteArrayInputStream(resultXml.getBytes());
            org.w3c.dom.Document doc = builder.parse(inputStream); //
            // 下面开始读取
            org.w3c.dom.Element root = doc.getDocumentElement(); // 获取根元素
            NodeList nl = root.getElementsByTagName("return_msg");
            org.w3c.dom.Element el = (org.w3c.dom.Element) nl.item(0);
            org.w3c.dom.Node nd = el.getFirstChild();
            nonceStr = nd.getNodeValue();
            return nonceStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //解析微信返回appid
    public static String getAppId(String resultXml) {
        String nonceStr;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = dbf.newDocumentBuilder();
            InputStream inputStream = new ByteArrayInputStream(resultXml.getBytes());
            org.w3c.dom.Document doc = builder.parse(inputStream); //
            // 下面开始读取
            org.w3c.dom.Element root = doc.getDocumentElement(); // 获取根元素
            NodeList nl = root.getElementsByTagName("appid");
            org.w3c.dom.Element el = (org.w3c.dom.Element) nl.item(0);
            org.w3c.dom.Node nd = el.getFirstChild();
            nonceStr = nd.getNodeValue();
            return nonceStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //解析微信返回mch_id
    public static String getMchId(String resultXml) {
        String nonceStr;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = dbf.newDocumentBuilder();
            InputStream inputStream = new ByteArrayInputStream(resultXml.getBytes());
            org.w3c.dom.Document doc = builder.parse(inputStream); //
            // 下面开始读取
            org.w3c.dom.Element root = doc.getDocumentElement(); // 获取根元素
            NodeList nl = root.getElementsByTagName("mch_id");
            org.w3c.dom.Element el = (org.w3c.dom.Element) nl.item(0);
            org.w3c.dom.Node nd = el.getFirstChild();
            nonceStr = nd.getNodeValue();
            return nonceStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //解析微信返回nonce_str
    public static String getNonceStr(String resultXml) {
        String nonceStr;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = dbf.newDocumentBuilder();
            InputStream inputStream = new ByteArrayInputStream(resultXml.getBytes());
            org.w3c.dom.Document doc = builder.parse(inputStream); //
            // 下面开始读取
            org.w3c.dom.Element root = doc.getDocumentElement(); // 获取根元素
            NodeList nl = root.getElementsByTagName("nonce_str");
            org.w3c.dom.Element el = (org.w3c.dom.Element) nl.item(0);
            org.w3c.dom.Node nd = el.getFirstChild();
            nonceStr = nd.getNodeValue();
            return nonceStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //解析微信返回prepay_id
    public static String getPrepayId(String resultXml) {
        String nonceStr;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = dbf.newDocumentBuilder();
            InputStream inputStream = new ByteArrayInputStream(resultXml.getBytes());
            org.w3c.dom.Document doc = builder.parse(inputStream); //
            // 下面开始读取
            org.w3c.dom.Element root = doc.getDocumentElement(); // 获取根元素
            NodeList nl = root.getElementsByTagName("prepay_id");
            org.w3c.dom.Element el = (org.w3c.dom.Element) nl.item(0);
            org.w3c.dom.Node nd = el.getFirstChild();
            nonceStr = nd.getNodeValue();
            return nonceStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    private static  int Interinteger(Float money){
    	if( null == money || money.equals(0f)){
    		return 0;
    	}
    	money = money * 100;
		if(money < 1 ){
			return 0;
		}
		/*四舍五入*/
		return Math.round(money);
    }
    
    public static String literalPcice(Float money){
    	int interinteger = Interinteger(money);
		return  0 >= interinteger? null: String.valueOf(interinteger);
    }
    
   
    
}